package kr.hkit.password.fragment.dialog;

import kr.hkit.password.R;
import kr.hkit.password.dao.InfoDao;
import kr.hkit.password.fragment.item.ItemWebView;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.Fragment;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.SearchView;

public class DialogSearch extends DialogFragment {
	private static final String TAG = "PW";
	private SearchView sv;
	private EditText mSearch;
	private InfoDao info;
	private Fragment fragment;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		Log.d(TAG, "DialogSearch onCreateView");
		View view = inflater.inflate(R.layout.dialog_search, container, false);
		
		info = (InfoDao) getActivity().getApplication();
		info.setMain(false);
		
		mSearch = (EditText) view.findViewById(R.id.search);
		sv = (SearchView) view.findViewById(R.id.searchView);
		sv.setOnSearchClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Bundle args = new Bundle();
				String url = mSearch.getText().toString();
				Log.d(TAG, "url : " + url);
				if (url.substring(0, 8) != "http://" || url.substring(0, 5) == "www.") {
					url = "http://" + url;
				}
				Log.d(TAG, "url : " + url);
				args.putString("url", url);
				fragment = new ItemWebView();
				fragment.setArguments(args);
				getFragmentManager().beginTransaction().replace(R.id.content_frame, fragment)
				.commit();
				dismiss();
			}
		});
		
		return view;
	}
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		Dialog dialog = new Dialog(getActivity());
		dialog.setContentView(R.layout.dialog_search);
		dialog.setTitle("검색");
		WindowManager.LayoutParams param = dialog.getWindow().getAttributes();
		param.width = 400;
		param.height = 200;
		return dialog;
	}
}
